$(function () {
    //1 获取所有数据
    $.ajax({
        url: "/hospital/perscription/all.action",
        type: "GET",
        dataType: "json",
        success: function (data) {
            var $list = $("#tb_perscrition");
            $.each(data, function (index, ele) {
                var $item = createDataItem(index, ele);
                $list.append($item);
            });
        },
        error: function () {
            alert("获取数据失败");
        }
    });

    //动态创建一条数据
    function createDataItem(index, data) {
        var $item = $("" + "<tr>\n" +
            "            <td style=\"width:100px;\">"+(index+1)+"</td>\n" +
            "            <td>" + data.patName + "</td>\n" +
            "            <td>" + data.mdcRecCreateDate + "</td>\n" +
            "            <td>" + data.mdcRecPatDescription + "</td>\n" +
            "            <td>" + data.mdcRecCheckpoint + "</td>\n" +
            "            <td>" + data.mdcRecResult + "</td>\n" +
            "            <td>" + data.mdcRecAdvice + "</td>\n" +
            "            <td>" + data.mdcRecPhysician + "</td>\n" +
            "            <td>\n" +
            "                <button data-id=\"" + data.id + "\" class=\"perscriptionInformationShowUpdateBtn\">编辑</button>\n" +
            "                <button data-id=\"" + data.id + "\" class=\"perscriptionInformationShowCancelBtn\">删除</button>\n" +
            "            </td>\n" +
            "        </tr>");
        return $item;
    }

    //动态刷新表格
    function createNewData(data) {
        //发送数据成功，返回成功数据，隐藏新增信息界面，显示表格数据
        var $list = $("#tb_perscrition");
        //1.清空表格数据
        $("#tb_perscrition tr:not(:first)").html("");
        //2.动态创建数据
        $.each(data, function (index, ele) {
            var $item1 = createDataItem(index, ele);
            $list.append($item1);
        });
    }

    //2. 新增信息按钮
    $("#perscriptionOperAddBtn").click(function () {
        $(".perscriptionInformation").css("display", "none");
        $(".perscriptionAdd").css("display", "block");
    });
    //2.1 确认按钮监听
    $("#perscriptionAddSureBtn").click(function () {
        //2.1.1 获取输入的数据
        var name = $("#perscriptionAddInputName").val();
        var date = $("#perscriptionAddInputDate").val();
        var descr = $("#perscriptionAddInputDescription").val();
        var check = $("#perscriptionAddInputPoint").val();
        var result = $("#perscriptionAddInputResult").val();
        var advice = $("#perscriptionAddInputAdvice").val();
        var physician = $("#perscriptionAddInputPerson").val();
        //2.1.2 传输数据
        $.ajax({
            url: "/hospital/perscription/add.action",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data: JSON.stringify({
                "patName": name,
                "mdcRecCreateDate": date,
                "mdcRecPatDescription": descr,
                "mdcRecCheckpoint": check,
                "mdcRecResult": result,
                "mdcRecAdvice": advice,
                "mdcRecPhysician": physician
            }),
            success: function (data) {
                createNewData(data);
                alert("添加成功");
                $(".perscriptionInformation").css("display", "block");
                $(".perscriptionAdd").css("display", "none");
            },
            error: function () {
                alert("添加失败");
            }
        });
    });
    //2.2 返回按钮点击
    $("#perscriptionAddBackBtn").click(function () {
        $(".perscriptionInformation").css("display", "block");
        $(".perscriptionAdd").css("display", "none");
    });

    //2.3 输入框失去焦点判断
    function judgeInput(value, judgeId, judgeName) {
        if (value.trim().length == 0) {
            $("#" + judgeId).html("/*" + judgeName + "不能为空*/");
        } else {
            $("#" + judgeId).remove();
        }
    }

    $("#perscriptionAddInputName").on("blur", function () {
        var value = $(this).val();
        judgeInput(value, "judgeName", "病人名称");
    });
    $("#perscriptionAddInputDate").on("blur", function () {
        var value = $(this).val();
        judgeInput(value, "judgeDate", "时间");
    });
    //3. 操作栏事件委托
    //3.1 编辑按钮事件委托监听
    $("#tb_perscrition").delegate(".perscriptionInformationShowUpdateBtn", "click", function () {
        var id = $(this).attr("data-id");
        // alert(id);
        //3.1.1 获取修改前的数据
        var value = $(this).parents("tr")[0];
        var value1 = value.children[1].innerHTML;
        var value2 = value.children[2].innerHTML;
        var value3 = value.children[3].innerHTML;
        var value4 = value.children[4].innerHTML;
        var value5 = value.children[5].innerHTML;
        var value6 = value.children[6].innerHTML;
        var value7 = value.children[7].innerHTML;
        $("#perscriptionName").val(value1);
        $("#perscriptionDate").val(value2);
        $("#perscriptionDescription").val(value3);
        $("#perscriptionPoint").val(value4);
        $("#perscriptionResult").val(value5);
        $("#perscriptionMdc").val(value6);
        $("#perscriptionPerson").val(value7);
        //显示编辑界面
        $(".perscroptionClickUpdateGetInfo").css("display", "block");
        $("#over").css("display", "block");
        //3.1.2 编辑框确认按钮监听事件
        $("#perscriptionclickUpdateGetInfoSureBtn").unbind("click").click(function () {
            //获取修改后的数据
            var name = $("#perscriptionName").val();
            var date = $("#perscriptionDate").val();
            var descr = $("#perscriptionDescription").val();
            var check = $("#perscriptionPoint").val();
            var result = $("#perscriptionResult").val();
            var advice = $("#perscriptionMdc").val();
            var physician = $("#perscriptionPerson").val();
            $.ajax({
                url: "/hospital/perscription/update.action",
                type: "POST",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                data: JSON.stringify({
                    "id": id,
                    "patName": name,
                    "mdcRecCreateDate": date,
                    "mdcRecPatDescription": descr,
                    "mdcRecCheckpoint": check,
                    "mdcRecResult": result,
                    "mdcRecAdvice": advice,
                    "mdcRecPhysician": physician
                }),
                success: function (data) {
                    createNewData(data);
                    alert("修改成功");
                    $(".perscroptionClickUpdateGetInfo").css("display", "none");
                    $("#over").css("display", "none");
                },
                error: function () {
                    alert("修改失败");
                }
            });
        });
        //3.1.3 编辑界面 返回按钮点击
        $("#perscriptionclickUpdateGetInfoBackBtn").click(function () {
            $(".perscroptionClickUpdateGetInfo").css("display", "none");
            $("#over").css("display", "none");
            $(".perscriptionInformation").css("display", "block");
        });
    });
    //删除按钮事件委托监听
    $("#tb_perscrition").delegate(".perscriptionInformationShowCancelBtn", "click", function () {
        var id = $(this).attr("data-id");
        // alert(id);
        $.ajax({
            url: "/hospital/perscription/delete.action",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data: JSON.stringify({"id": id}),
            success: function (data) {
                createNewData(data);
                alert("删除成功");
            },
            error: function () {
                alert("删除失败");
            }
        });
    });
});